package org.example.practicaltraining.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import javax.annotation.Resource;
import org.example.practicaltraining.commom.Result;
import org.example.practicaltraining.entity.item_credit;
import org.example.practicaltraining.mapper.Item_creditMapper;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/item")
@ResponseBody
public class ItemController {
    @Resource
    Item_creditMapper Item_creditMapper;

    // 获取商品列表
    @GetMapping("/getItemList")
    public Result<?> getItemList() {
        return Result.success(Item_creditMapper.selectList(null));
    }

    //获取推荐商品
    @GetMapping("/getRecommenedItem")
    public Result<?> getRecommenedItem() {
        LambdaQueryWrapper<item_credit> wrappers = Wrappers.<item_credit>lambdaQuery();
        wrappers.eq(item_credit::getRecommend,1);
        return Result.success(Item_creditMapper.selectList(wrappers));
    }

    // 获取商品详情
    @GetMapping("/getItemDetail/{id}")
    public Result<?> getItemDetail(@PathVariable("id") Integer id) {
        return Result.success(Item_creditMapper.selectById(id).getDetail());
    }
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "8") Integer pageSize){
        LambdaQueryWrapper<item_credit> wrappers = Wrappers.<item_credit>lambdaQuery();
        Page<item_credit> userPage =Item_creditMapper.selectPage(new Page<>(pageNum,pageSize), wrappers);
        return Result.success(userPage);
    }


}
